const path = require('path');
const fs = require('fs');

// 读取本地配置
async function getBrank() {
    var dirPath = path.join(__dirname, 'file');
    return await readFile('./bank2.log');
}

async function readFile(path) {
    return new Promise((resolve, reject) => {
        fs.readFile(path, 'UTF-8', (err, data) => {
            // console.log('err ', err, data);
            console.log(err);
            try {
                const _data = JSON.parse(data);
                // console.log(_data.data.length);
                resolve(_data);
            } catch (error) {
                resolve({});
            }
        })
    });
}
async function writeFile(path, text) {
    return new Promise((resolve, reject) => {
        fs.writeFile(path, text, {}, (err) => {
            if (err) {
                console.log('配置文件写入失败', err)
            } else {
                console.log('配置文件写入成功')
            }
            resolve();
        })

    });
}
(async function () {
    const banks = await getBrank();
    let num = 0;
    console.log('length: ', banks.data.length);
    const branch = [];
    banks.data.forEach(item => {
        // console.log(item.bankCode, item.cityCode);
        if (item.branch && item.branch.length) {
            num++;
            item.branch.forEach(_item => {
                branch.push(Object.assign({
                    bankCode: item.bankCode,
                    cityCode: item.cityCode
                }, _item));
            });
        }
    });
    writeFile('./bank3.log', JSON.stringify(branch));
    console.log('num: ', num);
    const bank = await readFile('./bank3.log');
    console.log(bank.length);
})();